<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------

namespace app\common\page;

use think\Paginator;

/**
 * Bootstrap 分页驱动
 */
class Bootstrap extends Paginator
{

    /**
     * 上一页按钮
     * @param string $text
     * @return string
     */
    protected function getPreviousButton(string $text = "{%pre page}"): string
    {

        if ($this->currentPage() <= 1) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url(
            $this->currentPage() - 1
        );

        return $this->getPageLinkWrapper($url, $text);
    }

    /**
     * 下一页按钮
     * @param string $text
     * @return string
     */
    protected function getNextButton(string $text = '{%next page}'): string
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url($this->currentPage() + 1);

        return $this->getPageLinkWrapper($url, $text);
    }

    /**
     * 页码按钮
     * @return string
     */
    protected function getLinks(): string
    {
        if ($this->simple) {
            return '';
        }

        $block = [
            'first'  => null,
            'slider' => null,
            'last'   => null,
        ];

        $side   = 3;
        $window = $side * 2;

        if ($this->lastPage < $window + 6) {
            $block['first'] = $this->getUrlRange(1, $this->lastPage);
        } elseif ($this->currentPage <= $window) {
            $block['first'] = $this->getUrlRange(1, $window + 2);
            $block['last']  = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
        } elseif ($this->currentPage > ($this->lastPage - $window)) {
            $block['first'] = $this->getUrlRange(1, 2);
            $block['last']  = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
        } else {
            $block['first']  = $this->getUrlRange(1, 2);
            $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
            $block['last']   = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
        }

        $html = '';

        if (is_array($block['first'])) {
            $html .= $this->getUrlLinks($block['first']);
        }

        if (is_array($block['slider'])) {
            $html .= $this->getDots();
            $html .= $this->getUrlLinks($block['slider']);
        }

        if (is_array($block['last'])) {
            $html .= $this->getDots();
            $html .= $this->getUrlLinks($block['last']);
        }

        return $html;
    }

    /**
     * 渲染分页html
     * @return mixed
     */
    public function render(bool $total=true,bool $pre=true,bool $links=true,bool $next=true,bool $listRow=true,bool $goPage=true)
    {
        if ($this->hasPages()) {
            $s1=$total?$this->getTotal():"";
            $s2=$pre?$this->getPreviousButton():"";
            $s3=$links?$this->getLinks():"";
            $s4=$next?$this->getNextButton():"";
            $s5=$listRow?$this->setListRow():"";
            $s6=$goPage?$this->goPage():"";
            $page_str= sprintf('<div class="layui-box layui-laypage layui-laypage-default">%s %s %s %s %s %s</div>',$s1,$s2,$s3,$s4,$s5,$s6);
            //语言变量替换
            return str_replace(
                    ["{%total}","{%pre page}","{%next page}","{%tiao}","{%tiao/page}","{%go/is}","{%page}","{%sure}","{%please enter the page number}"],
                    [lang('page.total'),lang('page.pre page'),lang('page.next page'),lang('page.tiao'),lang('page.tiao/page'),lang('page.go/is'),lang('page.page'),lang('page.sure'),lang('page.please enter the page number')], $page_str);
        }
    }
    //总计多少条记录
    public function getTotal(){
        return '<span class="layui-laypage-count">{%total} '.$this->total.' {%tiao}</span>';
    }
    //设置每页显示多少条
    public function setListRow(){
        $str="";
        foreach(config("app.page_row_inc") as $v){
            $str.='<option value="'.$this->setListRowParams($v).'" '.($v==$this->listRows?'selected':'').'>'.$v.' {%tiao/page}</option>';
        }
        return '<span class="layui-laypage-limits"><select lay-ignore onchange="location.href=this.options[this.selectedIndex].value">'
                . $str
                . '</select></span> ';
    }
    //设置每页显示多少条 跳转参数
    public function setListRowParams($listRow=20){
        $param=request()->get();
        $param['page']=1;//默认到第一页
        $param['listRow']=$listRow;
        $url = '?' . http_build_query($param, '', '&');
        return $url;
    }   
    //跳转到指定页面
    public function goPage(){
        $param=request()->get();
        unset($param["page"]);
        if($param){
            $url = '?' . http_build_query($param, '', '&')."&page=";
        }else{
            $url = '?page=';
        }
        return '<span class="layui-laypage-skip">{%go/is}<input type="text" value="'.request()->param('page').'" id="goPageId" min="1" value="1" class="layui-input">{%page}<button type="button" class="layui-laypage-btn" onclick="document.getElementById(\'goPageId\').value?(location.href=\''.$url.'\'+document.getElementById(\'goPageId\').value):alert(\'{%please enter the page number}\')">{%sure}</button></span>';
    }
    /**
     * 生成一个可点击的按钮
     *
     * @param  string $url
     * @param  string $page
     * @return string
     */
    protected function getAvailablePageWrapper(string $url, string $page): string
    {
        return '<a href="' . htmlentities($url) . '">' . $page . '</a>';
    }

    /**
     * 生成一个禁用的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getDisabledTextWrapper(string $text): string
    {
        return '<span><em class="layui-laypage-em"></em><em>' . $text . '</em></span>';
    }

    /**
     * 生成一个激活的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getActivePageWrapper(string $text): string
    {
        return '<span class="layui-laypage-curr"><em class="layui-laypage-em"></em><em>' . $text . '</em></span>';
    }

    /**
     * 生成省略号按钮
     *
     * @return string
     */
    protected function getDots(): string
    {
        return $this->getDisabledTextWrapper('...');
    }

    /**
     * 批量生成页码按钮.
     *
     * @param  array $urls
     * @return string
     */
    protected function getUrlLinks(array $urls): string
    {
        $html = '';

        foreach ($urls as $page => $url) {
            $html .= $this->getPageLinkWrapper($url, $page);
        }

        return $html;
    }

    /**
     * 生成普通页码按钮
     *
     * @param  string $url
     * @param  string    $page
     * @return string
     */
    protected function getPageLinkWrapper(string $url, string $page): string
    {
        if ($this->currentPage() == $page) {
            return $this->getActivePageWrapper($page);
        }

        return $this->getAvailablePageWrapper($url, $page);
    }
}
